---
#==============================================================#
# File      :   prometheus.yml
# Ctime     :   2020-04-03
# Mtime     :   2022-01-22
# Desc      :   Prometheus Configuration File
# Path      :   /etc/prometheus/prometheus.yml
# Copyright (C) 2018-2022 Ruohang Feng
#==============================================================#

#------------------------------------------------------------------------------
# Config FHS
#------------------------------------------------------------------------------
# /etc/prometheus/
#  ^-----prometheus.yml    # prometheus main config file
#  ^-----alertmanager.yml  # alertmanger main config file
#  ^-----@bin              # util scripts: check,reload,status,new
#  ^-----@rules            # record & alerting rules definition
#  ^
# /etc/prometheus/
#  ^-----@targets          # file based service discovery targets definition
#            ^-----@infra  # infra static targets definition
#            ^-----@nodes  # pgsql static targets definition
#            ^-----@pgsql  # pgsql static targets definition
#            ^-----@redis  # redis static targets definition (not exists for now)
#
#------------------------------------------------------------------------------


#------------------------------------------------------------------------------
# Globals
#------------------------------------------------------------------------------
global:
  scrape_interval: {{ prometheus_scrape_interval }}
  evaluation_interval: {{ prometheus_scrape_interval }}
  scrape_timeout: {{ prometheus_scrape_timeout }}


#------------------------------------------------------------------------------
# Alerts
#------------------------------------------------------------------------------
alerting:
  alertmanagers:
    - static_configs:
        - targets: # [ 127.0.0.1:9093 ]
            - {{ nginx_upstream | json_query('[?name==`alertmanager`].endpoint') | first }}
      scheme: http
      timeout: 10s
      api_version: v1


#------------------------------------------------------------------------------
# Rules
#------------------------------------------------------------------------------
rule_files:
  - rules/*.yml

{% if prometheus_sd_method == 'static' %}
#------------------------------------------------------------------------------
# Targets (Static File Service Discovery) (Default)
#------------------------------------------------------------------------------
# https://prometheus.io/docs/prometheus/latest/configuration/configuration/#file_sd_config
scrape_configs:

  #------------------------------------------------------------------------------
  # job: infra
  # targets: grafana | prometheus | altermanager | nginx | etc...
  # labels: [type, instance]
  # path: targets/infra/*.yml
  #------------------------------------------------------------------------------
  - job_name: infra
    metrics_path: /metrics
    file_sd_configs:
      - refresh_interval: {{ prometheus_sd_interval }}
        files: [ targets/infra/*.yml ]

  #------------------------------------------------------------------------------
  # job: nodes
  # node_exporter
  # labels: [ip]
  # path: targets/pgsql/*.yml
  #------------------------------------------------------------------------------
  - job_name: nodes
    metrics_path: {{ exporter_metrics_path }}
    file_sd_configs:
      - refresh_interval: {{ prometheus_sd_interval }}
        files: [ targets/nodes/*.yml ]

  #------------------------------------------------------------------------------
  # job: pgsql
  # node_exporter pg_exporter | pgbouncer_exporter | patroni | haproxy
  # labels: [cls, ins, ip, instance]
  # path: targets/pgsql/*.yml
  #------------------------------------------------------------------------------
  - job_name: pgsql
    metrics_path: {{ exporter_metrics_path }}
    file_sd_configs:
      - refresh_interval: {{ prometheus_sd_interval }}
        files: [ targets/pgsql/*.yml ]

  #------------------------------------------------------------------------------
  # job: redis
  # multiple redis targets from redis_exporter @ target nodes
  # labels: [cls, ip, ins, instance]
  # path: targets/redis/*.yml
  #------------------------------------------------------------------------------
  - job_name: redis
    metrics_path: /scrape
    file_sd_configs:
      - refresh_interval: {{ prometheus_sd_interval }}
        files: [ targets/redis/*.yml ]
    relabel_configs:
      - source_labels: [__address__]
        target_label: __param_target
      - source_labels: [__param_target]
        regex: ^redis://(.*):(\d+)$
        replacement: $1:$2
        target_label: instance
      - source_labels: [__param_target]
        regex: ^redis://(.*):(\d+)$
        replacement: $1
        target_label: ip
      # scrape redis_exporter on target node
      - source_labels: [__param_target]
        regex: ^redis://(.*):\d+$
        replacement: $1:9121
        target_label: __address__


{% endif %}

{% if prometheus_sd_method == 'consul' %}
#------------------------------------------------------------------------------
# Targets (Consul Service Discovery)
#------------------------------------------------------------------------------
# https://prometheus.io/docs/prometheus/latest/configuration/configuration/#consul_sd_config

scrape_configs:

  #------------------------------------------------------------------------------
  # job: infra (self monitoring)
  # grafana | prometheus | altermanager | nginx | etc...
  # labels: [type, instance]
  #------------------------------------------------------------------------------
  - job_name: infra
    metrics_path: /metrics
    consul_sd_configs:
      - server: 127.0.0.1:8500
        refresh_interval: {{ prometheus_sd_interval }}
        tags:
          - infra
          - exporter

    relabel_configs:
      # relabel meta.type -> type  (infra type)
      - source_labels: [ __meta_consul_service_metadata_type ]
        action: replace
        target_label: type


  #------------------------------------------------------------------------------
  # job: pgsql (database monitoring)
  # node_exporter | pg_exporter | pgbouncer_exporter | haproxy(exporter)
  # labels: [cls, ins, instance]
  # selector: tags @> [ pgsql, exporter ]
  #------------------------------------------------------------------------------
  - job_name: pgsql
    metrics_path: {{ exporter_metrics_path }}
    consul_sd_configs:
      - server: 127.0.0.1:8500
        refresh_interval: {{ prometheus_sd_interval }}
        tags:
          - pgsql
          - exporter

    relabel_configs:
      # relabel meta.cluster -> cls  (unique cluster identifier)
      - source_labels: [ __meta_consul_service_metadata_cluster ]
        action: replace
        target_label: cls

      # relabel meta.instance -> ins  (unique instance identifier prefixed with cls)
      - source_labels: [ __meta_consul_service_metadata_instance ]
        action: replace
        target_label: ins


{% endif %}


